<?php
/*
 * etc:用户基本模型层
 * author:deepe.xiao
 */

namespace app\models\Project_1_0;
use Core\Model;


class User extends Model{

    private $tableName = "t_user";


    /**
     * 查询用户 根据用户名字和密码
     * @param $user_id
     * @return mixed
     */
	function searchUser( $user,$pass  ){
		$sql = "select * from t_user where Name = '$user' and password ='$pass' limit 1 ";
//        $this->db->loadClass("db");
        $result = $this->db->getRow( $sql );
		return $result;
	}
    function userAll(  ){
        $sql = "select * from t_user  ";
//        $this->db->loadClass("db");
        $result = $this->db->fetch_sql( $sql );

        return $result;
    }


    /**
     * 查询用户 根据用户名
     * @param $user
     * @return mixed
     */
    function searchUserByName( $user  ){
        $sql = "select * from t_user where Name = '$user'limit 1 ";
        $result = $this->db->getRow( $sql );
        return $result;
    }
    /**
     * 修改用户密码 根据用户ID
     * @param $user_id
     * @return mixed
     */
    function updatePassByName( $name,$pass  ){
        $sql = "update t_user set password='$pass'  where Name = '$name'  ";
        $result = $this->db->exec_sql( $sql );
        return $result;
    }


    function register( $name,$pass){
        $sql = "insert into t_user(`Name`,`password`) values('$name','$pass')";
        $result = $this->db->exec_sql( $sql );
        return $result;
    }

    //为了HTML课程写的接口
    /**
     * @param $data 数组数据
     * @return int
     */
    function register2( $data){

        $result = $this->db->insert( $this->tableName,$data );
        return $result;
    }



    function registerByScore( $name,$score){
        $sql = "insert into t_user(`Name`,`password`,`score`) values('$name','123456','$score')";
        $result = $this->db->exec_sql( $sql );
        return $result;
    }

    /**
     * 查询积分前5的
     * @return mixed
     */
    function selectTop(){
        $sql = "select * from t_user order by score desc limit 5";
        $res = $this->db->fetch_sql($sql);
        return $res;
    }

    /**
     * 执行完成,返回执行的结果(1|0)
     * @return mixed
     */
    function updateScore($user_id,$score){
        $sql = "update t_user set score='$score' where user_id = $user_id;";
        $res = $this->db->exec_sql($sql);
        return $res;
    }

    /**
     * 查询用户的最高分数
     * @return mixed
     */
    function selectScoreByUserId($user_id){
        $sql = "select score from t_user where user_id=$user_id;";
        $res = $this->db->getRow($sql);
        return $res;
    }






}